﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Ship.DaDa
{
    /// <summary>
    /// 达达配送SDK
    /// </summary>
    public class SDK
    {

        #region 订单管理

        /// <summary>
        /// 查询订单运费接口
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public static string QueryFee(Dictionary<string, object> param)
        {
            return Request(Config.api_queryDeliverFee, param);
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        /// <returns></returns>
        public static string AddOrder(Dictionary<string, object> param)
        {
            return Request(Config.api_addorder, param);
        }

        /// <summary>
        /// 获取订单信息
        /// </summary>
        /// <param name="order_id"></param>
        /// <returns></returns>
        public static string GetOrder(string order_id)
        {
            var param = new Dictionary<string, object>();
            param.Add("order_id", order_id);
            return Request(Config.api_orderstatusquery, param);
        }

        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="order_id">订单号</param>
        /// <param name="cancel_reason_id">订单理由ID</param>
        /// <param name="cancel_reason">其它取消原因(当取消原因ID为其他时，此字段必填)</param>
        /// <returns></returns>
        public static string CacelOrder(string order_id, int cancel_reason_id, string cancel_reason = "")
        {
            var param = new Dictionary<string, object>();
            param.Add("order_id", order_id);
            param.Add("cancel_reason_id", cancel_reason_id);
            param.Add("cancel_reason", cancel_reason);
            return Request(Config.api_orderCancel, param);
        }

        /// <summary>
        /// 取消订单的理由
        /// </summary>
        /// <returns></returns>
        public static string GetCacelReasons()
        {
            return Request(Config.api_orderCancelReasons, "");
        }
        #endregion

        #region 门店管理
        /// <summary>
        /// 添加门店
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string AddShhop(List<Dictionary<string, object>> param)
        {
            return Request(Config.api_shopadd, param);
        }

        /// <summary>
        /// 更新门店
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string UpdateShhop(Dictionary<string, object> param)
        {
            return Request(Config.api_shopupdate, param);
        }


        /// <summary>
        /// 获取门店详情
        /// </summary>
        /// <param name="origin_shop_id"></param>
        /// <returns></returns>
        public static string GetShop(string origin_shop_id)
        {
            var param = new Dictionary<string, object>();
            param.Add("origin_shop_id", origin_shop_id);
            return Request(Config.api_shopdetail, param);
        }

        #endregion

        #region 余额管理

        /// <summary>
        /// 余额查询
        /// </summary>
        /// <param name="category">查询运费账户类型（1：运费账户；2：红包账户，3：所有），默认查询运费账户余额</param>
        public static string GetBalance(int category)
        {
            var param = new Dictionary<string, object>();
            param.Add("category", category);
            return Request(Config.api_balance, param);
        }

        #endregion

        #region 城市代码转换
       
        /// <summary>
        /// 获取城市代码
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string GetCityCode(Dictionary<string, object> param)
        {
            return Request(Config.api_city, param);
        }
       
        #endregion

        #region 私有方法

        /// <summary>
        /// 发起网络请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        static string Request(string url, object param)
        {
            var jsonParam = SerializeParam(param);
            return HttpHelper.GetHtml(string.Concat(Config.api_url, url), jsonParam);
        }
        
        /// <summary>
        /// 返回序列化的参数
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        static string SerializeParam(object param)
        {
            SortedDictionary<string, object> sortParam = new SortedDictionary<string, object>();
            sortParam.Add("body", Function.ToJson(param));      //业务参数
            sortParam.Add("format", "json");
            sortParam.Add("timestamp", Function.GetTimestamp());
            sortParam.Add("app_key", Config.app_key);
            sortParam.Add("v", "1.0");
            sortParam.Add("source_id", Config.source_id);

            sortParam.Add("signature", Function.GetSign(sortParam));    //签名
            return Function.ToJson(sortParam);
        }
        
        #endregion
    }
}